package com.hctg.forced2sleep.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;

import androidx.annotation.NonNull;

import com.actor.myandroidframework.utils.toaster.ToasterUtils;
import com.actor.myandroidframework.widget.BaseItemDecoration;
import com.blankj.utilcode.util.SizeUtils;
import com.hctg.forced2sleep.R;
import com.hctg.forced2sleep.adapter.PageScanAdapter;
import com.hctg.forced2sleep.databinding.ActivityPageScanBinding;
import com.hctg.forced2sleep.dialog.Tips2BtnDialog;
import com.hctg.forced2sleep.dialog.TipsIcon2BtnDialog;
import com.hctg.forced2sleep.service.MyAccessibilityService;
import com.hctg.forced2sleep.utils.AccessibilityUtils;
import com.hctg.forced2sleep.easy_window.AddMonitorEasyWindow;
import com.hjq.permissions.OnPermissionCallback;
import com.hjq.permissions.Permission;
import com.hjq.permissions.XXPermissions;
import com.hjq.window.EasyWindow;

import java.util.List;

/**
 * description: 首页 -> 监督 -> 页面监督
 * company    :
 * @author    : ldf
 * date       : 2024/7/29 on 16:59
 */
public class PageScanActivity extends BaseActivity<ActivityPageScanBinding> {

    private final PageScanAdapter mAdapter = new PageScanAdapter();

    public static void start(Context context) {
        context.startActivity(new Intent(context, PageScanActivity.class));
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        viewBinding.titleBar.tvTitle.setText("页面监督（限制）");
        viewBinding.titleBar.ivBack.setOnClickListener(v -> {
            if (viewBinding.titleBar.tvRight2.isSelected()) {
                viewBinding.titleBar.tvRight2.setSelected(false);
                viewBinding.titleBar.tvRight2.setText("删除");
                mAdapter.setIsShowCheckBox(false);
            } else {
                finish();
            }
        });
        //删除
        viewBinding.titleBar.setTvRight2("删除", v -> {
            if (v.isSelected()) {
                // TODO: 2024/7/30
                new Tips2BtnDialog(mActivity, "删除", "确认删除吗?", v1 -> {
                    ToasterUtils.warning("删除逻辑!");
                }).show();
            } else {
                v.setSelected(true);
                viewBinding.titleBar.tvRight2.setText("确认删除");
                mAdapter.setIsShowCheckBox(true);
            }
        });
        viewBinding.titleBar.setIvMore(R.drawable.right_icon_add, v -> {
            if (EasyWindow.existShowingByClass(AddMonitorEasyWindow.class)) {
                ToasterUtils.warning("请选择元素");
                return;
            }
            new TipsIcon2BtnDialog(mActivity, "温馨提示",
                    "页面监督是对app具体页面的监督控制，开启后，左上角会出现悬浮窗，你操作进入你要监督的页面，点击添加，即可实现某个页面的监督",
                    v1 -> {
                if (XXPermissions.isGranted(mActivity, Permission.SYSTEM_ALERT_WINDOW)) {
                    showGlobalWindow();
                } else {
                    //"添加监督"的悬浮窗
                    XXPermissions.with(mActivity)
                            .permission(Permission.SYSTEM_ALERT_WINDOW)
                            .request(new OnPermissionCallback() {
                                @Override
                                public void onGranted(@NonNull List<String> permissions, boolean allGranted) {
                                    viewBinding.getRoot().postDelayed(() -> {
                                        // 这里最好要做一下延迟显示，因为在某些手机（华为鸿蒙 3.0）上面立即显示会导致显示效果有一些瑕疵
                                        showGlobalWindow();
                                    }, 300L);
                                }
                                @Override
                                public void onDenied(@NonNull List<String> permissions, boolean doNotAskAgain) {
                                    new Tips2BtnDialog(mActivity, "权限", "请先授予悬浮窗权限, 才能引导页面监督!", v2 -> {
                                        XXPermissions.startPermissionActivity(mActivity, Permission.SYSTEM_ALERT_WINDOW);
                                    }).show();
                                }
                            });
                }
            }).show();
        });
        viewBinding.recyclerView.addItemDecoration(new BaseItemDecoration(0f, SizeUtils.dp2px(10f)));
        viewBinding.recyclerView.setAdapter(mAdapter);

        // TODO: 2024/7/29 delete!
        mAdapter.addData("");
        mAdapter.addData("");
        mAdapter.addData("");
        mAdapter.addData("");
        mAdapter.addData("");
    }


    /**
     * 显示全局弹窗
     */
    private void showGlobalWindow() {
        if (AccessibilityUtils.isAccessibilitySettingsOn(MyAccessibilityService.class)) {
            new AddMonitorEasyWindow().show();
        } else {
            AccessibilityUtils.go2AccessibilitySetting(mActivity, 0);
        }
    }
}